//
//  AdoptedPigsViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptedPigsViewController.h"
#import "AdoptedCell.h"
#import "TransferTypeViewController.h"
#import "AdoptedSelectTrusteeViewController.h"
#import "LookViewController.h"


@interface AdoptedPigsViewController ()

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) int currentPage;
@property (nonatomic, assign) int limit;

@end

@implementation AdoptedPigsViewController

#pragma mark - view life cycle

- (void)setupUI{
    
    self.view.backgroundColor = [UIColor clearColor];
    self.baseNavView.hidden = YES;
    [self initAction];
}

- (void)setupLayout{
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 0, 0));
    }];
}

- (void) initAction {
    //接收转让成功通知 刷新页面
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self.dataArr removeAllObjects];
        [self dataRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self dataRequest];
    }];
    self.tableView.mj_footer.hidden = YES;
}

//转让成功页面  点击回到领养按钮  s接受通知 刷新页面数据
- (void)refreshAdoptedData {
    [self.tableView.mj_header beginRefreshing];
}


- (void)initData {
    self.currentPage = 1;
    self.limit = 10;
}


#pragma mark - networking method

- (void)dataRequest {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    AdoptedParam *param = [[AdoptedParam alloc] init];
    param.memberId = memberId;
    param.limit = @(self.limit);
    param.page = @(self.currentPage);
    param.type = @(AdoptedTypeBuy);
    @weakify(self);
    [[[PigMarketApi sharedInstance] AdoptedListWithParam:param] subscribeNext:^(AdoptedList * _Nullable adoptedList) {
        @strongify(self);
        [self.dataArr addObjectsFromArray:adoptedList.list];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.currentPage == [adoptedList.totalPage intValue] || [adoptedList.totalPage intValue] == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.tableView.mj_footer.hidden = YES;
        }
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AdoptedCell *cell = [AdoptedCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        Adopted *model = self.dataArr[indexPath.row];
        [cell configCellWithModel:model adoptedType:AdoptedTypeBuy];
        if ([model.tags isEqualToString:@"已弃养"]) {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        @weakify(self);
        [cell setTransferBlock:^{
            @strongify(self);
            TransferTypeViewController *vc = [[TransferTypeViewController alloc] init];
            vc.adoptPigId = model.adoptPlanId;
            [self.navigationController pushViewController:vc animated:YES];
        }];
        [cell setLookBlock:^{
            @strongify(self);
            LookViewController *vc = [[LookViewController alloc] init];
            vc.pigId = @([model.pigId integerValue]);
            [self.navigationController pushViewController:vc animated:YES];
        }];
        [cell setPayBlock:^{
            @strongify(self);
            AdoptedSelectTrusteeViewController *vc = [[AdoptedSelectTrusteeViewController alloc] init];
            vc.adoptPlanId = model.adoptPlanId;
            vc.adoptType = AdoptedTypeBuy;
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - getter and setter

- (void)registerTableViewCell
{
    self.tableView.rowHeight = 242;
    self.tableView.backgroundColor = [UIColor clearColor];

}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

#pragma mark - notifications

- (void)addNotifications
{
    [kNotificationCenter addObserver:self selector:@selector(refreshAdoptedData) name:NotificationTransferChange object:nil];
}

- (void)removeNotifications
{
    [kNotificationCenter removeObserver:self name:NotificationTransferChange object:nil];
}

@end

